package com.jlastu.znb.presenter;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.jlastu.znb.R;
import com.jlastu.znb.contract.AddGoodsContract;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.entity.Photo;

import java.util.List;

public class AddGoodsPresenter implements AddGoodsContract.IAddGoodsPresenter {

   private AddGoodsContract.IAddGoodsView mAddGoodsView;
   private Context mContext;

   public AddGoodsPresenter(AddGoodsContract.IAddGoodsView addGoodsView, Context context) {
      mAddGoodsView = addGoodsView;
      mContext = context;
   }

   @Override
   public void saveGoods(Goods goods) {
      new Thread(new Runnable() {
         @Override
         public void run() {
            //商品插入
            ContentValues cv = new ContentValues();
            cv.put("goods_name", goods.getName());
            cv.put("goods_price", goods.getPrice());
            cv.put("goods_unit", goods.getUnit());
            cv.put("goods_message", goods.getMessage());
//            cv.put("goods_browse", 0);
            Uri goodsUrl = Uri.parse("content://com.jlastu.znb.provider/goods");
            Uri insertUri = mContext.getContentResolver().insert(goodsUrl, cv);
            long newRowId = ContentUris.parseId(insertUri);
            List<Photo> photos = goods.getPhotoList();
            for (Photo photo : photos) {
               ContentValues photoValues = new ContentValues();
               photoValues.put("goods_id", newRowId);
               photoValues.put("image", photo.getData()); // 确保图片数据被正确保存
               Uri photoUri = Uri.parse("content://com.jlastu.znb.provider/photo");
               mContext.getContentResolver().insert(photoUri, photoValues);
            }
         }
      }).start();
   }
}
